﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using QLTB.Data;
using QLTB.Entities;
using _4rumDCT.App_Code;

namespace QuanLyThietBi.DCTEDU_.DanhMuc
{
    public partial class DMCanBo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindCatalog.BindQuyenThanhVien(cboQuyenCanBo);
                Pager1.CurrentIndex = 1;
                Bind_Data(Pager1.CurrentIndex);
            }
        }

        protected void Bind_Data(int PageNo)
        {
            int TotalRows = 0;
            string WhereClause = "1=1";
            DataSet dsCanBo = DataRepository.Provider.ExecuteDataSet("CanBo_GetPaged", WhereClause, "NgayTao DESC", PageNo - 1, Pager1.PageSize);
            gridCatalog.DataSource = dsCanBo.Tables[0];
            gridCatalog.DataBind();
            try
            {
                TotalRows = Convert.ToInt32(dsCanBo.Tables[1].Rows[0][0]);
            }
            catch (Exception)
            {
            }
            Pager1.ItemCount = TotalRows;
            if (TotalRows <= Pager1.PageSize)
                Pager1.Visible = false;
            else
                Pager1.Visible = true;
        }

        protected void Pager1_Command(object sender, CommandEventArgs e)
        {
            try
            {
                int currnetPageIndex = Convert.ToInt32(e.CommandArgument);
                Pager1.CurrentIndex = currnetPageIndex;
                Bind_Data(Pager1.CurrentIndex);
            }
            catch (Exception ex)
            {
                Common.WriteLog("DMCanBo.aspx.cs", "Pager1_Command()", ex.ToString());
            }
        }

        protected void cmdSave_Click(object sender, EventArgs e)
        {
            bool updateStatus = false;
            try
            {
                CanBo entCanBo = new CanBo();
                entCanBo.MaCanBo = txtMaCanBo.Text;
                entCanBo.TenTruyCap = txtTenTruyCap.Text;
                entCanBo.HoTen = txtHoTen.Text;
                entCanBo.MaQuyen = Convert.ToInt32(cboQuyenCanBo.SelectedValue);
                entCanBo.NgayTao = Common.GetServerDateTime();
                
                if (DataRepository.CanBoProvider.GetByMaCanBo(entCanBo.MaCanBo)==null)
                {
                    entCanBo.MatKhau = txtMatKhau.Text;
                    updateStatus = DataRepository.CanBoProvider.Insert(entCanBo);
                }
                else
                {
                    if (txtMatKhau.Text == "")
                    {
                        entCanBo.MatKhau = H_MatKhau.Value;  
                    }
                    else
                    {
                        entCanBo.MatKhau = txtMatKhau.Text;
                    }
                    entCanBo.EntityState = EntityState.Changed;
                    entCanBo.OriginalMaCanBo = entCanBo.MaCanBo;

                    updateStatus = DataRepository.CanBoProvider.Update(entCanBo);
                }
                if (updateStatus)
                {
                    ScriptManager.RegisterStartupScript(upPanel1, this.GetType(), Constant.THANHCONG, Constant.CAPNHAT_THANHCONG, true);
                    cmdReset_Click(sender, e);
                }
                else
                    ScriptManager.RegisterStartupScript(upPanel1, this.GetType(), Constant.THATBAI, Constant.CAPNHAT_THATBAI, true);
            }
            catch (Exception ex)
            {
                Common.WriteLog("DMCanBo.aspx.cs", "cmdSave_Click", ex.ToString());
                ScriptManager.RegisterStartupScript(upPanel1, this.GetType(), Constant.THATBAI, Constant.CAPNHAT_THATBAI, true);
            }
        }

        protected void cmdReset_Click(object sender, EventArgs e)
        {
            Bind_Data(Pager1.CurrentIndex);
            txtHoTen.Text = "";
            cboQuyenCanBo.SelectedValue = "0";
            txtMaCanBo.Text = "";
            txtTenTruyCap.Text = "";
            H_MatKhau.Value = "";
            txtMatKhau.Text = "";

        }

        protected void gridCatalog_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                ImageButton l = (ImageButton)e.Row.FindControl("cmdDelete");
                l.Attributes.Add("onclick", "javascript:return " + "confirm('Bạn chắc chắn muốn xóa thông tin này?');");

                e.Row.Attributes["onmouseover"] = "this.style.cursor='hand';this.style.backgroundColor='WhiteSmoke';";
                e.Row.Attributes["onmouseout"] = "this.style.backgroundColor='White';";
            }
        }

        protected void gridCatalog_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Select")
            {
                try
                {
                    txtMaCanBo.Text = e.CommandArgument.ToString();
                    CanBo entCanBo = DataRepository.CanBoProvider.GetByMaCanBo(txtMaCanBo.Text);
                    if (entCanBo != null)
                    {
                        txtTenTruyCap.Text = entCanBo.TenTruyCap;
                        txtHoTen.Text = entCanBo.HoTen;
                        cboQuyenCanBo.SelectedValue = entCanBo.MaQuyen.ToString();
                        txtMatKhau.Text = entCanBo.MatKhau;
                        Bind_Data(Pager1.CurrentIndex);
                    }
                }
                catch (Exception ex)
                {
                    Common.WriteLog("DMLoaiChuyenMuc.aspx.cs", "gridCatalog_RowCommand - Select", ex.ToString());
                }
            }
            if (e.CommandName == "Delete")
            {
                try
                {
                    string IdCanBo = e.CommandArgument.ToString();
                    bool deleteStatus = DataRepository.CanBoProvider.Delete(IdCanBo);
                    if (!deleteStatus)
                        ScriptManager.RegisterStartupScript(upPanel1, this.GetType(), Constant.THATBAI, Constant.XOA_THATBAI, true);
                    else
                        ScriptManager.RegisterStartupScript(upPanel1, this.GetType(), Constant.THANHCONG, Constant.XOA_THANHCONG, true);
                    Bind_Data(Pager1.CurrentIndex);
                }
                catch (Exception ex)
                {
                    Common.WriteLog("DMCanBo.aspx.cs", "gridCatalog_RowCommand - Delete", ex.ToString());
                    ScriptManager.RegisterStartupScript(upPanel1, this.GetType(), Constant.THATBAI, Constant.XOA_THATBAI, true);
                }
            }
        }

        protected void gridCatalog_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            cmdReset_Click(sender, e);
        }

        public string GetQuyenCanBo(object Char)
        {
            int idQuyen = Convert.ToInt32(Char.ToString());
            using (QuyenCanBo entMenuTop = DataRepository.QuyenCanBoProvider.GetByMaQuyen(idQuyen))
            {
                return entMenuTop.TenQuyen;
            }
        }
    }
}